/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package peggame;

/**
 *
 * @author zach
 */
public class pegmanager {
    private int[][] board;
    private int centering_scheme=1;
    private int higest_scheme=2;
    private int default_scheme=2;
    private char[] indexvalues;
    public static int wall=1;
    public static int unvisited_path=0;
    public static int path_with_unvisted_parts=3;
    private int num=2000;
     int endx;
     int endy;
     int startx;
     int starty;

    public pegmanager(int[][] board,int startx,int starty,int endx,int endy) {
        this.board=new int[board.length][];
        this.endx=endx;
        this.endy=endy;
        for(int y=0;y<board.length;y++){
            this.board[y]=new int[board[y].length];
            for(int x=0;x<board[y].length;x++){
                this.board[y][x]=board[y][x];
            }
        }
    }
    public pegmanager(int[][] board,int scheme,int startx,int starty,int endx,int endy) {
        this(board,startx,starty,endx,endy);
        centering_scheme=scheme;
        sanity_check();
    }
     public pegmanager(int[][] board,int scheme,char[] index,int startx,int starty,int endx,int endy) {
        this(board,startx,starty,endx,endy);
        centering_scheme=scheme;
        sanity_check();
        indexvalues=index;
    }
     public pegmanager(int[][] board,char[] index,int startx,int starty,int endx,int endy) {
        this(board,startx,starty,endx,endy);
        indexvalues=index;
    }
    void sanity_check(){
        if(centering_scheme>higest_scheme)
            centering_scheme=default_scheme;
    }
    private int offset(int y1,int y2){
        int offset=0;
        if(centering_scheme>=1)
        offset=Math.max(board[y2].length, board[y1].length)-Math.min(board[y2].length, board[y1].length);
        if(centering_scheme==1)
            offset/=2;
        return offset;
    }
    public int getOffset(int y1,int y2){
         if(board[y1].length>board[y2].length)
            return offset(y1,y2);
        else
            return 0;
    }
    public boolean isinCol(int x1,int y1,int x2,int y2){
        int offset=offset( y1, y2);
        return Math.max(x2, x1)==(Math.min(x2, x1)+offset);
    }

    public boolean isinCol(int x1,int y1,int x2,int y2,int x3,int y3){
        return isinCol(x1,y1,x2,y2)&&isinCol(x1,y1,x3,y3)&&isinCol(x3,y3,x2,y2);
    }
    public boolean isinCol(int y,int x1,int x2,int x3,boolean t){
        return isinCol(x1,y,x2,y,x3,y);
    }
    public int NumPegs(){
        return num;
    }
    public void Jump(Move move){
        if(board[move.firsty][move.firstx]>1){
            board[move.firsty][move.firstx]--;
        }else {
           board[move.firsty][move.firstx]=1;
        }
        if(board[move.lasty+1][move.lastx]==unvisited_path||board[move.lasty+1][move.lastx]>=path_with_unvisted_parts)
            if(!(move.firsty==move.lasty+1&&move.firsty==move.lastx))
            board[move.lasty][move.lastx]++;
        else if(board[move.lasty][move.lastx+1]==unvisited_path||board[move.lasty][move.lastx+1]>=path_with_unvisted_parts)
            if(!(move.firsty==move.lasty&&move.firsty==move.lastx+1))
            board[move.lasty][move.lastx]++;
        else if(board[move.lasty][move.lastx-1]==unvisited_path||board[move.lasty][move.lastx-1]>=path_with_unvisted_parts)
            if(!(move.firsty==move.lasty&&move.firsty==move.lastx-1))
            board[move.lasty][move.lastx]++;
        else if(board[move.lasty-1][move.lastx]==unvisited_path||board[move.lasty-1][move.lastx]>=path_with_unvisted_parts)
            if(!(move.firsty==move.lasty-1&&move.firsty==move.lastx))
            board[move.lasty][move.lastx]++;

        if(move.lasty==endy&&move.lastx==endx)
            num=1;
    }

    public int hashCode(){
        int hashcode = 1;
        for(int y = 0; y < board.length; y++){
            for(int x=0;x<board[y].length;x++){
            if(board[y][x]==unvisited_path){
                hashcode *= 6449;
            }else if(board[y][x]==wall){
                hashcode *= 4153;
            } else {
                hashcode*=11;
            }
            hashcode++;
        }
        }
        return hashcode;
    }
    //@Override
   /* public int hashCode() {

         int i=0;
        String out="";
        for(boolean[] zig:board)
            for(boolean lain:zig){
                if(lain)
                    out+=indexvalues[i];
                i++;
            }
        return  ShiftAndAddHash(out);
    }*/
public int ShiftAndAddHash (String strValue)
{
    int hashCode = 0;
    long workHashCode = 0;

    if (strValue != null)
    {
        for (int counter=0; counter<strValue.length(); counter++)
        {
            workHashCode += (workHashCode + (counter % 4)) +
                       (int)strValue.charAt(counter);
        }
        workHashCode = workHashCode % (2010581);
    }
    hashCode = (int)workHashCode;

    return (hashCode);
}
    public int[][] getBoard() {
        return board;
    }

    public char[] getIndexvalues() {
        return indexvalues;
    }
    
}
